<template>
  <div class="app-container">
    <!--查询表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="24">
            <el-form-item label="日志名称">
              <el-input style="width: 100%" v-model="searchObj" placeholder="用户名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData(1)">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>

    <!-- 工具条 -->
    <div class="tools-div">
      <!-- 批量删除按钮 -->
      <el-button class="btn-add" size="mini" @click="batchRemove()">批量删除</el-button>
    </div>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%; margin-top: 10px"
      @selection-change="handleSelectionChange">

      <!-- 复选框 -->
      <el-table-column type="selection"/>
      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="username" label="日志名称"/>
      <el-table-column prop="ipaddr" label="登录IP地址"/>
      <el-table-column prop="status" label="登录状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1">成功</el-tag>
          <el-tag type="danger" v-else>失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="msg" label="提示信息"/>
      <el-table-column prop="accessTime" label="访问时间"/>
      <el-table-column prop="createTime" label="创建时间"/>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)"
                     title="删除"/>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

  </div>
</template>

<script>
import api from '@/api/loginLog/loginLog.js'

export default {
  name: 'LoginLogList',
  data() {
    return {
      listLoading: true, //  加载中的提示语.....
      list: [],
      total: 0,
      page: 1,
      limit: 5,
      searchObj: "",
      // 日志信息
      sysLoginLog: {},
      dialogVisible: false,
      // 选中的数据
      selectValueData: []
    };
  },
  created() {
    this.fetchData(this.page);
  },
  methods: {
    fetchData(pageNum) {
      this.page = pageNum;
      api.getLoginLogPageInfo(this.page, this.limit, this.searchObj).then(response => {
        this.list = response.data.records;
        this.total = response.data.total;
        this.listLoading = false;
      })
    },
    resetData() {
      // 清空表单
      this.searchObj = "";
      // 再次刷新列表数据
      this.fetchData(this.page);
    },
    removeDataById(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        api.removeLoginLogById(id)
          .then((response) => {
            // 提示
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // 刷新页面
            this.fetchData(this.page);
          });
      });
    },
    batchRemove() {
      // 判断是否有选中select
      if (this.selectValueData.length == 0) {
        this.$message.warning('请选择要删除的记录！')
        return;
      }
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let ids = this.selectValueData.map(item => item.id)
        api.removeLoginLogByIds(ids).then((response) => {
          // 提示
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 刷新页面
          this.fetchData(this.page);
        });
      });
    },
    handleSelectionChange(selectValue) {
      // console.log(selectValue);
      this.selectValueData = selectValue;
    }
  }
}
</script>

<style scoped lang="scss">

</style>
